package com.macro.mall.controller;

import com.macro.mall.common.annotation.AccessLimit;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.StorageObject;
import com.macro.mall.service.SmsFileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * MinIO对象存储管理Controller
 *
 * @author macro
 * @date 2019/12/25
 */
@Controller
@Api(tags = "MinioController")
@Tag(name = "MinioController", description = "对象存储文件上传删除")
@RequestMapping("/minio")
@AllArgsConstructor
public class MinioController {
    private final SmsFileStorageService smsFileStorageService;

    @AccessLimit
    @ApiOperation("文件上传")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<StorageObject> upload(@RequestPart("file") MultipartFile file) throws IOException {
        return CommonResult.success(smsFileStorageService.createSelective(file));
    }

    @AccessLimit
    @ApiOperation("文件删除")
    @RequestMapping(value = "/delete/{objId}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult<Object> delete(@PathVariable("objId") String objId) {
        return smsFileStorageService.deleteByObjId(objId) > 0 ? CommonResult.success(null) : CommonResult.failed();
    }
}
